from decorator import decorator
from pylons import request

def active_form_handler():
    def wrapper(func, self, *args, **kwargs):
        """Decorator Wrapper function"""
        print '%'*80
        
        #tear out the _ajax_form_parent_id
        print dir(request.environ.values)
        
        if '_ajax_form_parent_id' in kwargs:
            print 'here'
            del kwargs['_ajax_form_parent_id']
        try:
            value = func(self, *args, **kwargs)
            print value
        except errorType, e:
            print '*'*80
            print 'got here'
            #request.environ['pylons.routes_dict'] = error_handler
            request.statusText = 'FAILED_VALIDATION'
            #return self._perform_call(None, dict(url=error_handler))
        return value
    return decorator(wrapper)


from tg import TGController, context
from toscawidgets.api import WidgetBunch


class BaseController(TGController):
    """Base class for the root of a web application.
    
    Your web application should have one of these. The root of
    your application is used to compute URLs used by your app.
    """
    
    def __call__(self, environ, start_response):
        """Invoke the Controller"""
        # TGController.__call__ dispatches to the Controller method
        # the request is routed to. This routing information is
        # available in environ['pylons.routes_dict']

        # Create a container to send widgets to the template. Only those sent
        # in here will have their resources automatically included in the
        # template
        context.w = WidgetBunch()
        try:
            return TGController.__call__(self, environ, start_response)
        finally:
            pass
            #after everything is done clear out the Database Session
            #so to eliminate possible cross request DBSession polution.
#            model.DBSession.remove()
